import { Toast } from "antd-mobile"
import { reqcartdelete, reqcartedit, reqcartlist } from "../../http/api"

// state 
const initState = {
    arr: [],
    allChecked: false,
    // isRequest:true
}

// action type
const TYPES = {
    SHOP_CHANGE_ARR: "SHOP_CHANGE_ARR",
    SHOP_CHANGE_ALLCHECKED: "SHOP_CHANGE_ALLCHECKED",
    // SHOP_CHANGE_ISREQUEST:"SHOP_CHANGE_ISREQUEST"
}

// reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.SHOP_CHANGE_ARR:
            return {
                ...state,
                arr: action.arr
            }
        case TYPES.SHOP_CHANGE_ALLCHECKED:
            return {
                ...state,
                allChecked: action.allChecked
            }
        default:
            return state
    }
}
let isRequest = true;
// action creator
export const actions = {
    changeArr: arr => ({ type: TYPES.SHOP_CHANGE_ARR, arr }),
    changeAllChecked: allChecked => ({ type: TYPES.SHOP_CHANGE_ALLCHECKED, allChecked }),
    reqList: () => (dispatch, getState) => {
        let uid = getUid(getState())
        reqcartlist({ uid }).then(res => {
            if (res.data.code === 200) {
                let arr = res.data.list[1].content ? res.data.list[1].content : []
                let oldArr = getArr(getState())
                arr.forEach(item => {
                    let obj = oldArr.find(i => i.id === item.id)
                    item.checked = obj ? obj.checked : false
                })
                dispatch(actions.changeArr(arr))
                dispatch(actions.changeAllChecked(arr.length > 0 ? arr.every(item => item.checked) : false))
                isRequest = true;
            }
        })
    },
    add: id => (dispatch, getState) => {
        reqcartedit({ id, type: 2 }).then(res => {
            if (res.data.code === 200) {
                Toast.show({
                    content: res.data.msg
                })
                dispatch(actions.reqList())
            }
        })
    },
    sub: (id, num) => (dispatch, getState) => {
        if (num > 1 && isRequest) {
            isRequest = false;
            reqcartedit({ id, type: 1 }).then(res => {
                if (res.data.code === 200) {
                    Toast.show({
                        content: res.data.msg
                    })
                    dispatch(actions.reqList())
                }
            })
        } else {
            Toast.show({
                content: "宝贝不能再少了"
            })
        }
    },
    del: id => (dispatch) => {
        reqcartdelete({ id }).then(res => {
            if (res.data.code === 200) {
                Toast.show({
                    content: res.data.msg
                })
                dispatch(actions.reqList())
            }
        })
    },
    changeAll: () => (dispatch, getState) => {
        let arr = getArr(getState())
        let allChecked = getAll(getState())
        allChecked = !allChecked
        dispatch(actions.changeAllChecked(allChecked))
        arr.forEach(item => {
            item.checked = allChecked
        })
        dispatch(actions.changeArr(arr))
    },
    changeOne: index => (dispatch, getState) => {
        let arr = getArr(getState())
        arr[index].checked = !arr[index].checked
        dispatch(actions.changeArr(arr))
        dispatch(actions.changeAllChecked(arr.every(item => item.checked)))
    }
}

// selector导出数据给组件
const getUid = state => state.login.info.uid
export const getArr = state => state.shop.arr
export const getAll = state => state.shop.allChecked
export const getAllPrice = state => {
    let arr = getArr(state)
    return arr.reduce(
        (v, i) => (i.checked ? v + i.price * i.num : v),
        0
    )
}
// 导出reducer
export default reducer